package cn.edu.zut.internship.web.controller.sys;

import cn.edu.zut.internship.bean.sys.Result;
import cn.edu.zut.internship.bean.sys.SysUser;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class LoginController {

    @RequestMapping("/login")
    @ResponseBody
    public Result login(@RequestBody SysUser sysUser) {
        Result result = new Result();
        if (StringUtils.isEmpty(sysUser.getUsername()) || StringUtils.isEmpty(sysUser.getPassword())) {
            result.setCode(50008);
            result.setMessage("请输入用户名和密码！");
            return result;
        }
        //获取当前Subject 主体
        Subject subject = SecurityUtils.getSubject();
        //创建令牌
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(
                sysUser.getUsername(),
                sysUser.getPassword()
        );
        try {
            //进行验证，这里可以捕获异常，然后返回对应信息
            subject.login(usernamePasswordToken);
            //将用户信息放入Session中
            Session session = subject.getSession();
            session.setAttribute("userInfo",sysUser);
//            subject.checkRole("admin");
//            subject.checkPermissions("query", "add");
        } catch (UnknownAccountException e) {
            result.setCode(50008);
            result.setMessage("用户名不存在！");
            return result;
        } catch (AuthenticationException e) {
            result.setCode(50008);
            result.setMessage("账号或密码错误！");
            return result;
        } catch (AuthorizationException e) {
            result.setCode(50008);
            result.setMessage("没有权限！");
            return result;
        }
        result.setCode(20000);
        result.setMessage("登录成功");
        result.getBean().put("token",usernamePasswordToken);
        return result;
    }

    @RequiresRoles("admin")//可访问此接口的角色名
    @GetMapping("/admin")
    public String admin() {
        return "admin success!";
    }

    @GetMapping("/query")
    public String query() {
        return "query success!";
    }

    @GetMapping("/getUser")
    public String getUser() {
        return "getUser success!";
    }

    @RequiresPermissions("getRole")//可访问此角色的权限名
    @GetMapping("/getRole")
    public String getRole() {
        return "getRole success!";
    }

    @RequiresPermissions("query")
    @GetMapping("/index")
    public String index() {
        return "index success!";
    }

    @RequiresPermissions("add")
    @GetMapping("/add")
    public String add() {
        return "add success!";
    }
}

